<template>
  <div>
    <h2>组织架构</h2>
    <div v-loading="loading" class="container">
      <div class="header">
        <item-tree :data="componentData" :is-root="true" @addDepts="addDepts" />
      </div>
      <div class="item">
        <el-tree
          :data="list"
          node-key="id"
          :expand-on-click-node="false"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <item-tree slot-scope="{data}" :data="data" style="width: 100%" @editDepts="editDepts" @delDepart-success="getAllDepart" @addDepts="addDepts" />
        </el-tree>
      </div>
    </div>
    <!-- <addDepart v-model="isShowDiag" :node="node" @addDepts="getAllDepart" /> -->
    <addDepart ref="addDepts" :is-show-diag.sync="isShowDiag" :node="node" @addDepts="getAllDepart" />
  </div>
</template>

<script>
import itemTree from './components/itemTree'
import addDepart from './components/addDepart'
import { getAllList } from '@/api/department'
import { dataToTreeData } from '@/utils'
export default {
  name: 'Department',
  components: {
    itemTree,
    addDepart
  },
  data() {
    return {
      list: [],
      componentData: {},
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      node: null,
      isShowDiag: false,
      loading: false
    }
  },
  created() {
    this.getAllDepart()
  },
  methods: {
    async getAllDepart() {
      this.loading = true
      const { companyName, depts } = await getAllList()
      this.componentData = { name: companyName, manager: '负责人', id: '' }
      this.list = dataToTreeData(depts, '')
      this.loading = false
    },
    // 添加子节点
    addDepts(node) {
      this.node = node
      // 打开弹窗
      this.isShowDiag = true
    },
    // 编辑子部门
    editDepts(node) {
      // 修改node为编辑的部门
      this.node = node
      // 获取当前节点的部门信息，应该点击了编辑部门的按钮了之后再去获取部门的相关信息
      this.$refs.addDepts.getDeptsInfo(node.id)
      // 弹出会话框
      console.log('111')
      this.isShowDiag = true
    }
  }
}
</script>

<style scoped lang="scss">
.container {
  // margin: 30px 20px;
  padding: 30px  140px;
  font-size: 14px;
  box-shadow: 5px 5px 15px rgba(0,0,0,.4);
  .item {
    margin-top: 15px;
  }
}
</style>
